﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Sisocana.Core;
using Sisocana.Core.Models;
using Sisocana.Core.Interfaces;
using Sisocana.Dal.EntityModels;
using Sisocana.Dal;
using Sisocana.Dal.Interfaces;
using System.Data;

namespace Sisocana.Core
{
    public class FuenteService : ServiceBase<IFuenteModel>, IFuenteService 
    {
        public FuenteService()
            : this(new FuenteRepository())
        {
        }

        private IFuenteRepository _fuenteRepository;
        public FuenteService(IFuenteRepository fuenteRepository)
        {
            _fuenteRepository = fuenteRepository ?? new FuenteRepository();
        }

        // REDES SOCIALES

        public void createRedesSocialesMapperEF2Model()
        {
            AutoMapper.Mapper.CreateMap<RedesSociales, RedesSocialesModel>()
              .ForMember(dest => dest.descripcion, opt => opt.MapFrom(src => src.descripcion.Trim()))
              .ForMember(dest => dest.nombre, opt => opt.MapFrom(src => src.nombre.Trim()))
              .ForMember(dest => dest.pagWeb, opt => opt.MapFrom(src => src.pagWeb.Trim()))
              .ForMember(dest => dest.logo, opt => opt.MapFrom(src => src.logo.imgByte))
              .ForMember(dest => dest.logoNomb, opt => opt.MapFrom(src => src.logo.imgNombre))
              .ForMember(dest => dest.logoExt, opt => opt.MapFrom(src => src.logo.imgExt));
        }

        public void createRedesSocialesMapperModel2EF()
        {
            AutoMapper.Mapper.CreateMap<RedesSocialesModel, RedesSociales>()
              .ForMember(dest => dest.nombre, opt => opt.MapFrom(src => src.nombre))
              .ForMember(dest => dest.descripcion, opt => opt.MapFrom(src => src.descripcion))
              .ForMember(dest => dest.pagWeb, opt => opt.MapFrom(src => src.pagWeb));
        }

        public int addRedesSociales(RedesSocialesModel rs)
        {

            RedesSociales rsEF = new RedesSociales();
            rsEF.nombre = rs.nombre;
            rsEF.pagWeb = rs.pagWeb;
            rsEF.descripcion = rs.descripcion;
            rsEF.logo.imgByte = rs.logo;
            rsEF.logo.imgExt = rs.logoExt;
            rsEF.logo.imgNombre = rs.logoNomb;
            _fuenteRepository.Add(rsEF);

            try
            {
                _fuenteRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }

        }

        public int updateRedesSociales(RedesSocialesModel rs)
        {
            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<RedesSociales> rssEF = fuentesEF.OfType<RedesSociales>();
            RedesSociales rsEF = rssEF.First(a => a.nombre.Trim() == rs.nombre.Trim());
            rsEF.logo.imgByte = rs.logo;
            rsEF.logo.imgExt = rs.logoExt;
            rsEF.logo.imgNombre = rs.logoNomb;
            rsEF.pagWeb = rs.pagWeb;
            rsEF.descripcion = rs.descripcion;
            try{
                _fuenteRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }
            
        }

        public int deleteRedesSociales(RedesSocialesModel rs)
        {
            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<RedesSociales> rssEF = fuentesEF.OfType<RedesSociales>();
            RedesSociales rsEF = rssEF.First(a => a.nombre.Trim() == rs.nombre.Trim());

            if (rsEF.RedSocialAsociado.Count != 0)
                return -1;

            try
            {
                _fuenteRepository.Delete(rsEF);
                _fuenteRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }

        }

        public RedesSocialesModel getSigleRedesSociales(Expression<Func<RedesSocialesModel, bool>> whereCondition)
        {
            createRedesSocialesMapperEF2Model();

            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<RedesSociales> rsEF = fuentesEF.OfType<RedesSociales>();

            List<RedesSocialesModel> rs = new List<RedesSocialesModel>();

            rs = AutoMapper.Mapper.Map(rsEF, rs);

            IQueryable<RedesSocialesModel> rsRet = rs.AsQueryable<RedesSocialesModel>().Where(whereCondition);

            List<RedesSocialesModel> ret = rsRet.ToList();

            if (ret.Count == 0)
                return null;

            RedesSocialesModel rsFinal = ret.First();
            //rsFinal.nombre = ret.First().nombre.Trim();
            //if (ret.First().pagWeb != null)
                //rsFinal.pagWeb = ret.First().pagWeb.Trim();
            //rsFinal.descripcion = ret.First().descripcion.Trim();
            return rsFinal;
        }

        public IList<RedesSocialesModel> GetAllRedesSociales()
        {
            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<RedesSociales> redesEF = fuentesEF.OfType<RedesSociales>();

            createRedesSocialesMapperEF2Model();

            IList<RedesSocialesModel> redes = new List<RedesSocialesModel>();
            redes = AutoMapper.Mapper.Map(redesEF, redes);

            return redes;
        }

        //FIN REDES SOCIALES

        // IMAGENES

        public void createImagenesMapperEF2Model()
        {
            AutoMapper.Mapper.CreateMap<Imagenes, ImagenModel>()
              .ForMember(dest => dest.descripcion, opt => opt.MapFrom(src => src.descripcion.Trim()))
              .ForMember(dest => dest.nombre, opt => opt.MapFrom(src => src.nombre.Trim()))
              .ForMember(dest => dest.pagWeb, opt => opt.MapFrom(src => src.pagWeb.Trim()))
              .ForMember(dest => dest.logo, opt => opt.MapFrom(src => src.logo.imgByte))
              .ForMember(dest => dest.logoNomb, opt => opt.MapFrom(src => src.logo.imgNombre))
              .ForMember(dest => dest.logoExt, opt => opt.MapFrom(src => src.logo.imgExt));
        }

        public void createImagenesMapperModel2EF()
        {
            AutoMapper.Mapper.CreateMap<ImagenModel, Imagenes>()
              .ForMember(dest => dest.nombre, opt => opt.MapFrom(src => src.nombre))
              .ForMember(dest => dest.descripcion, opt => opt.MapFrom(src => src.descripcion))
              .ForMember(dest => dest.pagWeb, opt => opt.MapFrom(src => src.pagWeb));
        }

        public int addImage(ImagenModel img)
        {

            Imagenes imgEF = new Imagenes();
            imgEF.nombre = img.nombre;
            imgEF.pagWeb = img.pagWeb;
            imgEF.descripcion = img.descripcion;
            imgEF.logo.imgByte = img.logo;
            imgEF.logo.imgExt = img.logoExt;
            imgEF.logo.imgNombre = img.logoNomb;
            _fuenteRepository.Add(imgEF);

            try
            {
                _fuenteRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }

        }

        public int updateImage(ImagenModel rs)
        {
            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<Imagenes> rssEF = fuentesEF.OfType<Imagenes>();
            Imagenes rsEF = rssEF.First(a => a.nombre.Trim() == rs.nombre.Trim());
            rsEF.logo.imgByte = rs.logo;
            rsEF.logo.imgExt = rs.logoExt;
            rsEF.logo.imgNombre = rs.logoNomb;
            rsEF.pagWeb = rs.pagWeb;
            rsEF.descripcion = rs.descripcion;
            try
            {
                _fuenteRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }

        }

        public int deleteImage(ImagenModel rs)
        {
            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<Imagenes> rssEF = fuentesEF.OfType<Imagenes>();
            Imagenes rsEF = rssEF.First(a => a.nombre.Trim() == rs.nombre.Trim());

            if (rsEF.ImagenAsociado.Count != 0)
                return -1;

            try
            {
                _fuenteRepository.Delete(rsEF);
                _fuenteRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }

        }

        public ImagenModel getSigleImagen(Expression<Func<ImagenModel, bool>> whereCondition)
        {
            createImagenesMapperEF2Model();

            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<Imagenes> imgEF = fuentesEF.OfType<Imagenes>();

            List<ImagenModel> img = new List<ImagenModel>();

            img = AutoMapper.Mapper.Map(imgEF, img);

            IQueryable<ImagenModel> imgRet = img.AsQueryable<ImagenModel>().Where(whereCondition);

            List<ImagenModel> ret = imgRet.ToList();

            if (ret.Count == 0)
                return null;

            return ret.First();
        }

        public IList<ImagenModel> GetAllImagenes()
        {
            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<Imagenes> imagenesEF = fuentesEF.OfType<Imagenes>();

            createImagenesMapperEF2Model();

            IList<ImagenModel> imagenes = new List<ImagenModel>();
            imagenes = AutoMapper.Mapper.Map(imagenesEF, imagenes);

            return imagenes;
        }

        //FIN IMAGENES

        //VIDEOS

        public void createVideoMapperEF2Model()
        {
            AutoMapper.Mapper.CreateMap<Video, VideoModel>()
              .ForMember(dest => dest.descripcion, opt => opt.MapFrom(src => src.descripcion.Trim()))
              .ForMember(dest => dest.nombre, opt => opt.MapFrom(src => src.nombre.Trim()))
              .ForMember(dest => dest.pagWeb, opt => opt.MapFrom(src => src.pagWeb.Trim()))
              .ForMember(dest => dest.logo, opt => opt.MapFrom(src => src.logo.imgByte))
              .ForMember(dest => dest.logoNomb, opt => opt.MapFrom(src => src.logo.imgNombre))
              .ForMember(dest => dest.logoExt, opt => opt.MapFrom(src => src.logo.imgExt));
        }

        public void createVideoMapperModel2EF()
        {
            AutoMapper.Mapper.CreateMap<VideoModel, Video>()
              .ForMember(dest => dest.nombre, opt => opt.MapFrom(src => src.nombre))
              .ForMember(dest => dest.descripcion, opt => opt.MapFrom(src => src.descripcion))
              .ForMember(dest => dest.pagWeb, opt => opt.MapFrom(src => src.pagWeb));
        }

        public int addVideo(VideoModel video)
        {

            Video videoEF = new Video();
            videoEF.nombre = video.nombre;
            videoEF.pagWeb = video.pagWeb;
            videoEF.descripcion = video.descripcion;
            videoEF.logo.imgByte = video.logo;
            videoEF.logo.imgExt = video.logoExt;
            videoEF.logo.imgNombre = video.logoNomb;
            _fuenteRepository.Add(videoEF);

            try
            {
                _fuenteRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }

        }

        public int updateVideo(VideoModel rs)
        {
            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<Video> rssEF = fuentesEF.OfType<Video>();
            Video rsEF = rssEF.First(a => a.nombre.Trim() == rs.nombre.Trim());
            rsEF.logo.imgByte = rs.logo;
            rsEF.logo.imgExt = rs.logoExt;
            rsEF.logo.imgNombre = rs.logoNomb;
            rsEF.pagWeb = rs.pagWeb;
            rsEF.descripcion = rs.descripcion;
            try
            {
                _fuenteRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }

        }

        public int deleteVideo(VideoModel rs)
        {
            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<Video> rssEF = fuentesEF.OfType<Video>();
            Video rsEF = rssEF.First(a => a.nombre.Trim() == rs.nombre.Trim());

            if (rsEF.VideoAsociado.Count != 0)
                return -1;

            try
            {
                _fuenteRepository.Delete(rsEF);
                _fuenteRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }

        }

        public VideoModel getSigleVideo(Expression<Func<VideoModel, bool>> whereCondition)
        {
            createVideoMapperEF2Model();

            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<Video> vidEF = fuentesEF.OfType<Video>();

            List<VideoModel> vid = new List<VideoModel>();

            vid = AutoMapper.Mapper.Map(vidEF, vid);

            IQueryable<VideoModel> vidRet = vid.AsQueryable<VideoModel>().Where(whereCondition);

            List<VideoModel> ret = vidRet.ToList();

            if (ret.Count == 0)
                return null;

            return ret.First();
        }

        public IList<VideoModel> GetAllVideo()
        {
            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<Video> videosEF = fuentesEF.OfType<Video>();

            createVideoMapperEF2Model();

            IList<VideoModel> videos = new List<VideoModel>();
            videos = AutoMapper.Mapper.Map(videosEF, videos);

            return videos;
        }

        // FIN VIDEO


//*****************************************************************************************
        //NOTICIAS

        public void createNoticiaMapperEF2Model()
        {
            AutoMapper.Mapper.CreateMap<Noticias, NoticiaModel>()
              .ForMember(dest => dest.logo, opt => opt.MapFrom(src => src.logo.imgByte))
              .ForMember(dest => dest.logoNomb, opt => opt.MapFrom(src => src.logo.imgNombre))
              .ForMember(dest => dest.logoExt, opt => opt.MapFrom(src => src.logo.imgExt))
              .ForMember(dest => dest.feedurl, opt => opt.MapFrom(src => src.url))
              .ForMember(dest => dest.logoURL, opt => opt.MapFrom(src => src.logo.imgNombre));
        }

        public void createNoticiaMapperModel2EF()
        {
            AutoMapper.Mapper.CreateMap<NoticiaModel, Noticias>()
              .ForMember(dest => dest.nombre, opt => opt.MapFrom(src => src.nombre))
              .ForMember(dest => dest.descripcion, opt => opt.MapFrom(src => src.descripcion))
              .ForMember(dest => dest.pagWeb, opt => opt.MapFrom(src => src.pagWeb))
              .ForMember(dest => dest.url, opt => opt.MapFrom(src => src.feedurl))
              .ForMember(dest => dest.logo.imgNombre, opt => opt.MapFrom(src => src.logoURL));
        }

        public int addNoticia(NoticiaModel noticia)
        {

            Noticias noticiaEF = new Noticias();
            noticiaEF.url = noticia.feedurl;
            noticiaEF.nombre = noticia.nombre;
            noticiaEF.pagWeb = noticia.pagWeb;
            if (noticia.descripcion == null)
                noticia.descripcion = "";
            noticiaEF.descripcion = noticia.descripcion;
            noticiaEF.logo.imgByte = noticia.logo;
            noticiaEF.logo.imgExt = noticia.logoExt;
            noticiaEF.logo.imgNombre = noticia.logoURL;
            _fuenteRepository.Add(noticiaEF);

          //  try
          //  {
                _fuenteRepository.Save();
                return 0;
          //  }
          //  catch (Exception e)
          //  {
          //      return e.InnerException.GetHashCode();
          //  }

        }

        public int updateNoticia(NoticiaModel noticiaModel)
        {
            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<Noticias> noticiasEF = fuentesEF.OfType<Noticias>();
            Noticias noticiaEF = noticiasEF.First(a => a.nombre.Trim() == noticiaModel.nombre.Trim());
            noticiaEF.logo.imgByte = noticiaModel.logo;
            noticiaEF.logo.imgExt = noticiaModel.logoExt;
            noticiaEF.logo.imgNombre = noticiaModel.logoURL;
            noticiaEF.pagWeb = noticiaModel.pagWeb;
            noticiaEF.descripcion = noticiaModel.descripcion;
            try
            {
                _fuenteRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }

        }

        public int deleteNoticia(NoticiaModel noticiaModel)
        {
            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<Noticias> noticiasEF = fuentesEF.OfType<Noticias>();
            Noticias noticiaEF = noticiasEF.First(a => a.nombre.Trim() == noticiaModel.nombre.Trim());

            if (noticiaEF.NoticiaAsociado.Count != 0)
                return -1;

            try
            {
                _fuenteRepository.Delete(noticiaEF);
                _fuenteRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }

        }

        public NoticiaModel getSigleNoticia(Expression<Func<NoticiaModel, bool>> whereCondition)
        {
            createNoticiaMapperEF2Model();

            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<Noticias> noticiasEF = fuentesEF.OfType<Noticias>();

            IList<NoticiaModel> noticias = new List<NoticiaModel>();

            noticias = AutoMapper.Mapper.Map(noticiasEF, noticias);

            IQueryable<NoticiaModel> noticiasRet = noticias.AsQueryable<NoticiaModel>().Where(whereCondition);

            List<NoticiaModel> ret = noticiasRet.ToList();

            if (ret.Count == 0)
                return null;

            NoticiaModel fuenteNoticia = ret.First();
            fuenteNoticia.obtenerInformacion();

            return fuenteNoticia;
        }

        public IList<NoticiaModel> GetAllNoticia()
        {
            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<Noticias> noticiasEF = fuentesEF.OfType<Noticias>();

            createNoticiaMapperEF2Model();

            IList<NoticiaModel> noticias = new List<NoticiaModel>();
            noticias = AutoMapper.Mapper.Map(noticiasEF, noticias);

            foreach (NoticiaModel item in noticias)
            {
                //item.obtenerInformacion();
            }
            return noticias;
        }

        // FIN NOTICIA

//*****************************************************************************************
      public FuenteModel GetSingle(Expression<Func<FuenteModel, bool>> whereCondition)
        {
            /*createSitioMapperEF2Model();

            IList<Sitio> sitiosEF = _siteRepository.GetAll();
            List<FuenteModel> sitios = new List<FuenteModel>();

            sitios = AutoMapper.Mapper.Map(sitiosEF, sitios);

            IQueryable<FuenteModel> sitioRet = sitios.AsQueryable<FuenteModel>().Where(whereCondition);

            List<FuenteModel> ret = sitioRet.ToList();
            
             if (ret.Count == 0)
                return null;

            return ret.First();*/
            return null;
        }


        public void Add(FuenteModel entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(FuenteModel entity)
        {
            throw new NotImplementedException();
        }

        public void Update(FuenteModel entity)
        {
            throw new NotImplementedException();
        }

        public IList<FuenteModel> GetAll(Expression<Func<FuenteModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public IList<FuenteModel> GetAll()
        {
            /*createSitioMapperEF2Model();

            IList<Sitio> sitiosEF = _siteRepository.GetAll();
            List<FuenteModel> sitios = new List<FuenteModel>();
            foreach (Sitio sitEF in sitiosEF)
            {
                FuenteModel sit = new FuenteModel();
                sit = AutoMapper.Mapper.Map(sitEF, sit);
                sitios.Add(sit);
            }
            return sitios;*/
            return null;
        }

        public IQueryable<FuenteModel> Query(Expression<Func<FuenteModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count(Expression<Func<FuenteModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count()
        {
            throw new NotImplementedException();
        }
    }
}
  